package cn.jeeweb.web.modules.sys.controller;

import cn.jeeweb.common.utils.FastJsonUtils;
import cn.jeeweb.web.aspectj.annotation.Log;
import cn.jeeweb.web.modules.sys.service.ISysUserConfigService;
import cn.jeeweb.web.modules.sys.entity.SysUserConfig;
import cn.jeeweb.common.http.Response;
import cn.jeeweb.common.mvc.annotation.ViewPrefix;
import cn.jeeweb.common.mvc.controller.BaseBeanController;
import cn.jeeweb.common.mybatis.mvc.wrapper.EntityWrapper;
import cn.jeeweb.common.query.annotation.PageableDefaults;
import cn.jeeweb.common.query.data.PropertyPreFilterable;
import cn.jeeweb.common.query.data.Queryable;
import cn.jeeweb.common.query.utils.QueryableConvertUtils;
import cn.jeeweb.common.security.shiro.authz.annotation.RequiresMethodPermissions;
import cn.jeeweb.web.utils.PageRequest;
import cn.xjzn.framework_Core.ModuleActionType;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * @package cn.jeewe.bbs.controller
 * @title: sys_user_config控制器
 * @description: sys_user_config控制器
 */

@RestController
@RequestMapping("/sys/userConfig")
@ViewPrefix("modules/SysUserConfig")
//@RequiresPathPermission("sys:userConfig")
@Log(title = "sys_user_config")
public class SysUserConfigController extends BaseBeanController<SysUserConfig> {
	@Autowired
	private ISysUserConfigService sysUserConfigService;


	@GetMapping
	@RequiresMethodPermissions("view")
	public ModelAndView list(Model model, HttpServletRequest request, HttpServletResponse response) {
		return displayModelAndView("list");
	}

	/**
	 * 根据页码和每页记录数，以及查询条件动态加载数据
	 *
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "ajaxList", method = { RequestMethod.GET, RequestMethod.POST })
	@PageableDefaults(sort = "id=desc")
	@Log(logType = ModuleActionType.SELECT)
//	@RequiresMethodPermissions("list")
	public void ajaxList(PropertyPreFilterable propertyPreFilterable, HttpServletRequest request,HttpServletResponse response) throws IOException {
		EntityWrapper<SysUserConfig> entityWrapper = new EntityWrapper<>(entityClass);
		propertyPreFilterable.addQueryProperty(new String[]{"id","name","code","value","remarks","createBy","createDate","updateBy","updateDate","delFlag"});
		// 预处理
		SerializeFilter filter = propertyPreFilterable.constructFilter(entityClass);
		// 预处理
		Page pageBean = sysUserConfigService.selectPage(PageRequest.getPage(),entityWrapper);
		FastJsonUtils.print(pageBean);
//		return Response.ok(null,pageBean);
	}

	@GetMapping(value = "add")
	public ModelAndView add(Model model, HttpServletRequest request, HttpServletResponse response) {
		model.addAttribute("data", new SysUserConfig());
		return displayModelAndView ("edit");
	}

	@PostMapping("add")
	@Log(logType = ModuleActionType.INSERT)
//	@RequiresMethodPermissions("add")
	public Response add(SysUserConfig entity, BindingResult result,
						   HttpServletRequest request, HttpServletResponse response) {
		// 验证错误
		this.checkError(entity,result);
		sysUserConfigService.insert(entity);
		return Response.ok("添加成功");
	}

	@GetMapping(value = "{id}/update")
	public ModelAndView update(@PathVariable("id") String id, Model model, HttpServletRequest request,
							   HttpServletResponse response) {
		SysUserConfig SysUserConfig = sysUserConfigService.selectById(id);
		model.addAttribute("data", SysUserConfig);
		return displayModelAndView ("edit");
	}

	@PostMapping("{id}/update")
	@Log(logType = ModuleActionType.UPDATE)
//	@RequiresMethodPermissions("update")
	public Response update(SysUserConfig entity, BindingResult result,
						   HttpServletRequest request, HttpServletResponse response) {
		// 验证错误
		this.checkError(entity,result);
		sysUserConfigService.insertOrUpdate(entity);
		return Response.ok("更新成功");
	}

	@PostMapping("{id}/delete")
	@Log(logType = ModuleActionType.DELETE)
//	@RequiresMethodPermissions("delete")
	public Response delete(@PathVariable("id") String id) {
		sysUserConfigService.deleteById(id);
		return Response.ok("删除成功");
	}

	@PostMapping("batch/delete")
	@Log(logType = ModuleActionType.DELETE)
//	@RequiresMethodPermissions("delete")
	public Response batchDelete(@RequestParam("ids") String[] ids) {
		List<String> idList = java.util.Arrays.asList(ids);
		sysUserConfigService.deleteBatchIds(idList);
		return Response.ok("删除成功");
	} 
}